package zdar.entities;

import java.util.Random;

/**
 * This class will perform the main card drawing functions for the game, including initiative draws and dealing with the devil.
 * Units will have to draw cards themselves because they may have perks which let them let them raise the value of a card to a min value or whatever
 * @author danbye50
 *
 */
public class CardDeck {	
	
	private int[] deck;
	private int draws;
	
	public CardDeck(){
		deck = new int[54];
		draws = 0;
		for (int i=0; i<deck.length; i++){
			deck[i] = i + 1;
		}
		shuffle();
	}
	
	public int draw(){
		/*/ For now let the error occur so that we know when this terrible thing happens
		if (draws < 54){
			draws++;
			return deck[draws - 1];
		} else {
			shuffle();
			draws = 1;
			return deck[draws];
		}//*/
		draws++;
		return deck[draws - 1];
	}
	
	public void shuffle(){
		// This shuffler works the same as taking a random card and putting it into a new pile until all cards are in the new, shuffled pile
		// To use less resources and make things harder to read, this method does it all in the same pile. Whatever, it works.
		int index, temp;
		Random r = new Random();
		for (int i=(deck.length-1); i>0; i--){
			index = r.nextInt(i + 1);	// draw a random card from the remaining undrawn portion of the deck
			temp = deck[index];			// hold on to this card
			deck[index] = deck[i];		// take card at the bottom of the untouched pile and move it into the random index
			deck[i] = temp;				// put the random card at the bottom of the untouched pile (which is now the top of the shuffled pile)
		}
	}
	
}
